/*
    Copyright (C) 2008 Edwin Lee

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

var HTTPGET_RSPTYPE_TEXT_IDX = 0;
var HTTPGET_RSPTYPE_XML_IDX = 1;
var HTTPGET_RSPTYPE_STREAM_IDX = 2;

KeyValuePair = function(key, value)
{
	var keyValuePair = new Object();
	
	keyValuePair.key = key;
	keyValuePair.value = value;
	
	keyValuePair.toString = function()
	{
		return (this.key + "=" + this.value);
	}
	
	return keyValuePair;
}

httpGet = function(url, bustCache)
{
	var xmlHttpRequest = new XMLHttpRequest();
	
	var response = new Array();
	response[0] = null;
	response[1] = null;
	response[2] = null;
	
	if (bustCache)
	{
		url += ((url.search("\\?") == -1) ? ("?bustCache=" + Math.random()) : ("&bustCache=" + Math.random()));
	}
	
	xmlHttpRequest.open("GET", url, false);
	
	try
	{
		xmlHttpRequest.send(null);
		
		if (xmlHttpRequest.status == 200)
		{
			try
			{
				response[0] = xmlHttpRequest.responseText;
			}
			catch (err)
			{
				// response[0] = null
			}
			
			try
			{
				response[1] = xmlHttpRequest.responseXml;
			}
			catch (err)
			{
				// response[1] = null
			}
			
			try
			{
				response[2] = xmlHttpRequest.responseStream;
			}
			catch (err)
			{
				// response[2] = null
			}
		}
	}
	catch (err)
	{
		// Just Return Array of NULLs
	}
	
	return response;
};

httpGetWithParams = function(url, keyValuePairs, bustCache)
{
	var callUrl = url;
	
	if (keyValuePairs)
	{
		for (var i = 0; i < keyValuePairs.length; i++)
		{
			if (i == 0)
			{
				callUrl += "?";
			}
			else
			{
				callUrl += "&";
			}
			
			callUrl += keyValuePairs[i].toString();
		}
	}
	
	return (httpGet(callUrl, bustCache));
}

